<template>
  <div class="wins-bg">
    <div class="wins-center">
      <div class="wins-card">
        <div class="wins-header">
          <div class="wins-title">
            <div class="medal-icon">
              <el-icon size="32"><Medal /></el-icon>
            </div>
            <span>我的中奖记录</span>
          </div>
          <div class="decoration">
            <div class="diamond"></div>
            <div class="diamond"></div>
            <div class="diamond"></div>
          </div>
        </div>

        <el-form :model="form" class="wins-form" @submit.prevent>
          <el-form-item label="用户名" prop="username">
            <el-input
                v-model="form.username"
                placeholder="请输入用户名"
                clearable
                class="custom-input"
                @keyup.enter="getWins"
            />
          </el-form-item>
          <el-form-item>
            <el-button
                type="primary"
                @click="getWins"
                style="width: 100%;"
                class="query-button"
            >
              查询记录
            </el-button>
          </el-form-item>
        </el-form>

        <div class="wins-content">
          <div v-if="winList.length" class="wins-list">
            <div
                v-for="(win, index) in winList"
                :key="win.id"
                class="win-item"
                :style="{ animationDelay: index * 0.1 + 's' }"
            >
              <div class="win-badge">
                <el-icon><Medal /></el-icon>
              </div>
              <div class="win-details">
                <div class="prize-name">{{ win.prizeName }}</div>
                <div class="win-time">{{ formatDate(win.winTime) }}</div>
              </div>
              <div class="win-decoration">
                <div class="sparkle"></div>
                <div class="sparkle"></div>
              </div>
            </div>
          </div>
          <div v-else class="empty-state">
            <div class="trophy-icon">
              <el-icon size="48"><Trophy /></el-icon>
            </div>
            <p>暂无中奖记录</p>
            <span>快去参与抽奖吧，好运等着你！</span>
          </div>
        </div>

        <div class="wins-link">
          <el-link type="primary" @click="$emit('to-lottery')" underline="never">
            <el-icon><Back /></el-icon> 返回抽奖
          </el-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import request from '../api/request'
import { Medal, Trophy, Back } from '@element-plus/icons-vue'

const emit = defineEmits(['to-register', 'to-lottery', 'to-wins'])

const form = ref({ username: '' })
const winList = ref([])

const getWins = async () => {
  if (!form.value.username) {
    winList.value = []
    return
  }
  try {
    const res = await request.get(`/lottery/my-wins?username=${form.value.username}`)
    winList.value = res.data
  } catch (e) {
    winList.value = []
  }
}

const formatDate = (dateString) => {
  const date = new Date(dateString)
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}
</script>

<style scoped>
.wins-bg {
  min-height: 100vh;
  background: linear-gradient(135deg, #fff5e6 0%, #ffe6cc 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
}

.wins-center {
  width: 100vw;
  display: flex;
  align-items: center;
  justify-content: center;
}

.wins-card {
  background: rgba(255, 255, 255, 0.92);
  border-radius: 20px;
  box-shadow: 0 15px 35px rgba(255, 153, 95, 0.2);
  padding: 40px 35px 30px;
  width: 420px;
  max-width: 95vw;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 184, 108, 0.3);
  position: relative;
  overflow: hidden;
}

.wins-card::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(255, 184, 108, 0.15) 0%, transparent 70%);
  z-index: -1;
}

.wins-header {
  text-align: center;
  margin-bottom: 30px;
}

.wins-title {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28px;
  font-weight: bold;
  color: #ff9a3d;
  gap: 12px;
  margin-bottom: 15px;
}

.medal-icon {
  background: linear-gradient(135deg, #ff9a3d, #ff6a00);
  border-radius: 50%;
  padding: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 0 20px rgba(255, 154, 61, 0.5);
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% { box-shadow: 0 0 10px rgba(255, 154, 61, 0.5); }
  50% { box-shadow: 0 0 25px rgba(255, 154, 61, 0.8); }
  100% { box-shadow: 0 0 10px rgba(255, 154, 61, 0.5); }
}

.decoration {
  display: flex;
  justify-content: center;
  gap: 15px;
}

.diamond {
  width: 12px;
  height: 12px;
  background: linear-gradient(135deg, #ff9a3d, #ff6a00);
  transform: rotate(45deg);
  animation: blink 1.5s infinite;
}

.diamond:nth-child(2) {
  animation-delay: 0.5s;
}

.diamond:nth-child(3) {
  animation-delay: 1s;
}

@keyframes blink {
  0%, 100% { opacity: 0.6; }
  50% { opacity: 1; }
}

.wins-form {
  margin-bottom: 25px;
}

.custom-input :deep(.el-input__wrapper) {
  background: rgba(255, 245, 230, 0.6);
  border: 1px solid rgba(255, 184, 108, 0.5);
  border-radius: 10px;
  box-shadow: none;
  transition: all 0.3s ease;
}

.custom-input :deep(.el-input__wrapper:hover) {
  border-color: rgba(255, 154, 61, 0.8);
}

.custom-input :deep(.el-input__wrapper.is-focus) {
  border-color: #ff9a3d;
  box-shadow: 0 0 0 2px rgba(255, 154, 61, 0.2);
}

.custom-input :deep(.el-input__inner) {
  color: #333;
  background: transparent;
}

:deep(.el-form-item__label) {
  color: #666;
  font-weight: 500;
}

.query-button {
  background: linear-gradient(135deg, #ff9a3d 0%, #ff6a00 100%);
  border: none;
  color: white;
  font-weight: bold;
  letter-spacing: 1px;
  transition: all 0.3s ease;
  box-shadow: 0 5px 15px rgba(255, 154, 61, 0.4);
}

.query-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(255, 154, 61, 0.6);
}

.wins-content {
  min-height: 200px;
}

.wins-list {
  max-height: 300px;
  overflow-y: auto;
  padding-right: 5px;
}

.wins-list::-webkit-scrollbar {
  width: 6px;
}

.wins-list::-webkit-scrollbar-thumb {
  background: rgba(255, 154, 61, 0.5);
  border-radius: 3px;
}

.win-item {
  background: rgba(255, 245, 230, 0.6);
  border-radius: 12px;
  padding: 15px;
  margin-bottom: 15px;
  display: flex;
  align-items: center;
  gap: 15px;
  border: 1px solid rgba(255, 184, 108, 0.5);
  position: relative;
  overflow: hidden;
  animation: fadeInUp 0.5s ease forwards;
  opacity: 0;
  transform: translateY(20px);
}

@keyframes fadeInUp {
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.win-item:hover {
  background: rgba(255, 235, 210, 0.8);
  border-color: rgba(255, 154, 61, 0.8);
  transform: translateY(-3px);
  transition: all 0.3s ease;
  box-shadow: 0 5px 15px rgba(255, 153, 95, 0.2);
}

.win-badge {
  background: linear-gradient(135deg, #ff9a3d, #ff6a00);
  border-radius: 50%;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  flex-shrink: 0;
  box-shadow: 0 0 10px rgba(255, 154, 61, 0.5);
}

.win-details {
  flex-grow: 1;
}

.prize-name {
  color: #333;
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 5px;
}

.win-time {
  color: #666;
  font-size: 13px;
}

.win-decoration {
  display: flex;
  gap: 5px;
}

.sparkle {
  width: 6px;
  height: 6px;
  background: #ffcc00;
  border-radius: 50%;
  animation: sparkle 1s infinite;
}

.sparkle:nth-child(2) {
  animation-delay: 0.5s;
}

@keyframes sparkle {
  0%, 100% { opacity: 0.4; transform: scale(1); }
  50% { opacity: 1; transform: scale(1.5); }
}

.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: #999;
}

.trophy-icon {
  margin-bottom: 20px;
  color: #ff9a3d;
  opacity: 0.7;
}

.empty-state p {
  font-size: 18px;
  margin-bottom: 10px;
  color: #666;
  font-weight: 500;
}

.empty-state span {
  font-size: 14px;
  color: #999;
}

.wins-link {
  margin-top: 25px;
  text-align: center;
}

:deep(.el-link) {
  color: #ff9a3d;
  font-weight: 500;
  transition: all 0.3s ease;
}

:deep(.el-link:hover) {
  color: #ff6a00;
  transform: scale(1.05);
}

:deep(.el-link .el-icon) {
  margin-right: 5px;
}

@media (max-width: 480px) {
  .wins-card {
    padding: 30px 20px;
  }

  .wins-title {
    font-size: 24px;
  }
}
</style>